package com.baidu.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baidu.model.entity.PhotoModelWithBLOBs;
import com.baidu.response.ResponseBody;
import com.baidu.service.InPhotoControllerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.until.pagination.PaginationRequest;
import com.until.response.RestfulCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Objects;

/**
 * @author user
 */
@Service
public class PhotoControllerServiceImpl implements InPhotoControllerService{
    @Autowired
    private RestfulCode restfulCode;

    @Autowired
    private ResponseBody responseBody;

    @Autowired
    private PhotoServiceImpl photoService;

    @Override
    public RestfulCode.CodeSatCode phoneUpLoad(Integer orders, Integer albumId, String name, MultipartFile file, PhotoModelWithBLOBs photoModel) throws Exception {
        RestfulCode.CodeSatCode userActionFailed = null;

        photoModel.setContent(file.getBytes());
        String filename=file.getOriginalFilename();
        photoModel.setContentType(Objects.requireNonNull(filename).substring(filename.lastIndexOf(".")));
        photoModel.setCreateTime(new Date());
        photoModel.setOrders(orders);
        photoModel.setName(name);
        photoModel.setAlbumId(albumId);

        if (photoService.phoneUpLoad(photoModel)>0) {
            userActionFailed = responseBody.USER_OPERATION_SUCCEEDED;
        }else{
            userActionFailed=responseBody.USER_ACTION_FAILED;
            userActionFailed.setMessage("用户上传图片失败");
        }
        return restfulCode.getFreeResult(userActionFailed);
    }

    @Override
    public void getPhotoBilbo(Integer id, OutputStream output) throws Exception {
        PhotoModelWithBLOBs photo = photoService.selectByFileId(id);
        output.write(photo.getContent());
        output.close();
    }

    @Override
    public RestfulCode.CodeSatCode getPhotoBilboInfo(Integer id, HttpServletRequest request) throws Exception {
        RestfulCode.CodeSatCode userActionFailed = responseBody.USER_OPERATION_SUCCEEDED;
        PhotoModelWithBLOBs photo = photoService.selectByFileId(id);
        userActionFailed.setData(JSON.toJSON(photo));
        return userActionFailed;
    }

    @Override
    public RestfulCode.CodeSatCode getPhotoBilboInfoByAlbumId(Integer id, Integer orders, HttpServletRequest request, PaginationRequest page) {
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());
        RestfulCode.CodeSatCode userActionFailed = responseBody.USER_OPERATION_SUCCEEDED;
        PageInfo<JSONObject> objectPageInfo = PageHelper.startPage(page.getPageNumber(), page.getPageSize()).doSelectPageInfo(() ->
                photoService.selectByAlbumId(id, orders,request)
        );
        userActionFailed.setData(JSON.toJSON(objectPageInfo));
        return userActionFailed;
    }

    @Override
    public RestfulCode.CodeSatCode deletePhotoById(Integer id) {
        RestfulCode.CodeSatCode userActionFailed = null;
        try {
            int successfully = photoService.deletePhotoById(id);
            userActionFailed=successfully>0?responseBody.USER_OPERATION_SUCCEEDED:responseBody.NOT_FOUND_EXAMPLE;
        }catch (Exception e){
            userActionFailed=responseBody.INTERNAL_SERVER_ERROR;
        }
        return userActionFailed;
    }
}
